﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using HMIS.Infrastructure.Core.Authority;
using HMIS.FrameWork;
using HMIS.MVC.CommonSupport;
using HMIS.Infrastructure.Facade.Authority;
using HMIS.MVC.ViewModel;
using Iesi.Collections.Generic;
using System.ComponentModel;
using HMIS.Infrastructure.Core.Authority.Services;

namespace HMIS.MVC.Controllers
{
    public class InfraRoleController : BaseController
    {
        private readonly IRoleFacade RoleFacade;
        private readonly IMenuFacade MenuFacade;
        private readonly IActionPermissionFacade ActionPermissionFacade;

        public InfraRoleController(IRoleFacade RoleFacade, IMenuFacade MenuFacade, IActionPermissionFacade ActionPermissionFacade)
        {
            this.RoleFacade = RoleFacade;
            this.MenuFacade = MenuFacade;
            this.ActionPermissionFacade = ActionPermissionFacade;
        }

        [Description("访问角色管理页面")]
        [ViewPage]
        public ActionResult Role()
        {
            return View();
        }

        [Description("获取角色列表")]
        public ActionResult GetPlist()
        {
            long total;
            var data = RoleFacade.GetPlist(
				int.Parse(Request["start"] == null ? "0" : Request["start"]),
				int.Parse(Request["limit"] == null ? "0" : Request["limit"]),
                Request["sort"],
                Request["dir"],
                this.DesDataFilter(Request["filter"]),
                out total);
            return this.JsonFormat(
                new ExtGirdData<Role> { total = total, data = data });
        }

        [ExtResult]
        [Description("判断角色的名称、编码是否在类别下唯一")]
        public ActionResult IsFieldExist()
        {
            var result = RoleFacade.IsFieldExist(Request["FieldName"], Request[Request["FieldName"]], Request["Id"]);
            return this.JsonFormat(new ExtResult { success = !result, msg = "已存在相同的项!" });
        }
        [ExtResult]
        [Description("添加、修改角色")]
        public ActionResult SaveOrUpdate()
        {
            Role entity;
            if (String.IsNullOrEmpty(Request["Id"]))
            {
                entity = new Role
                {
                    Name = Request["Name"],
                    Code = Request["Code"],
                    State = (RoleState)int.Parse(Request["State"]),
                    Description = Request["Description"],
                };

                string inheritRoleId = Request["InheritRole.Id"];
                RoleFacade.SaveRole(entity, inheritRoleId);
            }
            else
            {
                entity = RoleFacade.Get(Request["Id"]);
                entity.State = (RoleState)int.Parse(Request["State"]);
                entity.Description = Request["Description"];
                RoleFacade.SaveOrUpdate(entity);
            }

            
            return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
        }
        [ExtResult]
        [Description("删除角色")]
        public ActionResult Delete()
        {
            RoleFacade.Delete(Request["Id"]);
            return this.JsonFormat((new ExtResult { success = true }));
        }
        public ActionResult GetRole()
        {
            var entity = RoleFacade.Get(Request["Id"]);
            return this.JsonFormat(
                new ExtFormLoadData<Role> { data = new Role[] { entity } }
                );
        }
        //返回职务角色
        [Description("返回职务角色")]
        public ActionResult GetRoleState()
        {
            var result = EnumDescription.ToList<RoleState>();
            return this.JsonFormat(result);
        }

        [Description("获取菜单的数据源")]
        public ActionResult GetMenuGrid()
        {
            var data = MenuFacade.GetAlllist();
            return this.JsonFormat(
                new ExtGirdData<MenuNode> { total = 0, data = data });
        }

        [Description("获取功能权限列表")]
        public ActionResult GetPlistActionPermission()
        {
            long total;
            var data = ActionPermissionFacade.GetPlistAll(
                int.Parse(Request["start"]),
                int.Parse(Request["limit"]),
                Request["sort"],
                Request["dir"],
                this.DesDataFilter(Request["filter"]),
                out total);
            return this.JsonFormat(
                new ExtGirdData<ActionPermission> { total = total, data = data });
        }

        [Description("根据角色ID获取关联菜单集合")]
        public ActionResult GetMenuListByRoleId()
        {
            //Role role = RoleFacade.GetByIdWithMenuAndPermissions(Request["Id"]);
            IList<MenuNode> menuNodes = MenuFacade.GetMenuListByRoleId(Request["Id"]);
            return this.JsonFormat(
                new ExtGirdData<MenuNode> { total = 0, data = menuNodes });
        }


        [Description("根据角色ID获取关联功能权限集合")]
        public ActionResult GetActionPermissionsListByRoleId()
        {
            //Role role = RoleFacade.GetByIdWithMenuAndPermissions(Request["Id"]);
            IList<ActionPermission> actionPermissions = ActionPermissionFacade.GetActionPermissionListByRoleId(Request["Id"]);
            return this.JsonFormat(
                new ExtGirdData<ActionPermission> { total = 0, data = actionPermissions });
        }

        [ExtResult]
        [Description("添加关联菜单对照项")]
        public ActionResult SaveMenu()
        {
            Role Role = RoleFacade.GetByIdWithMenuAndPermissions(Request["Id"]);
            string[] MIds = Request["MId"].Split(',');
            for (int i = 0; i < MIds.Length; i++)
            {
                if (!string.IsNullOrEmpty(MIds[i]))
                {
                    MenuNode MenuNode = MenuFacade.Load(MIds[i]);
                    Role.MenuNodes.Add(MenuNode);
                    RoleFacade.SaveOrUpdate(Role);

                }
            }
            return this.JsonFormat((new ExtResult { success = true }));
        }

        [ExtResult]
        [Description("删除关联菜单对照项")]
        public ActionResult DeleteMenu()
        {
            Role Role = RoleFacade.GetByIdWithMenuAndPermissions(Request["Id"]);
            string[] MIds = Request["MId"].Split(',');
            for (int i = 0; i < MIds.Length; i++)
            {
                if (!string.IsNullOrEmpty(MIds[i]))
                {
                    MenuNode MenuNode = MenuFacade.Load(MIds[i]);
                    Role.MenuNodes.Remove(MenuNode);
                    RoleFacade.SaveOrUpdate(Role);

                }
            }
            return this.JsonFormat((new ExtResult { success = true }));
        }

        [ExtResult]
        [Description("添加关联功能权限对照项")]
        public ActionResult SaveActionPermissions()
        {
            Role Role = RoleFacade.GetByIdWithMenuAndPermissions(Request["Id"]);
            string[] MIds = Request["PId"].Split(',');
            for (int i = 0; i < MIds.Length; i++)
            {
                if (!string.IsNullOrEmpty(MIds[i]))
                {
                    ActionPermission ActionPermission = ActionPermissionFacade.LoadActionPermission(MIds[i]);
                    Role.ActionPermissions.Add(ActionPermission);
                    RoleFacade.SaveOrUpdate(Role);

                }
            }
            return this.JsonFormat((new ExtResult { success = true }));

        }

        [ExtResult]
        [Description("删除关功能权限单对照项")]
        public ActionResult DeleteActionPermissions()
        {
            Role Role = RoleFacade.GetByIdWithMenuAndPermissions(Request["Id"]);
            string[] MIds = Request["PId"].Split(',');
            for (int i = 0; i < MIds.Length; i++)
            {
                if (!string.IsNullOrEmpty(MIds[i]))
                {
                    ActionPermission ActionPermission = ActionPermissionFacade.LoadActionPermission(MIds[i]);
                    Role.ActionPermissions.Remove(ActionPermission);
                    RoleFacade.SaveOrUpdate(Role);

                }
            }
            return this.JsonFormat((new ExtResult { success = true }));
        }
    }
}
